
package handlers;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import readers.QueryStringHandler;
import validator.validPW;
import validator.validUserName;
import writers.UserWriter;

/**
 * This handler will add a student
 * 
 * As of v1.01> this will now check the student exists
 * @author Chris
 */
public class addStudentHandler extends HttpServlet {
    static void failAdd(HttpServletRequest request, HttpServletResponse response)
    throws IOException {
        response.sendRedirect("addStudent.jsp?failed=t");
    }
    
    //This will check for a valid student and add them to the group if
    //possible
    protected void process(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String user = getUserName(request, response);
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String pass1 = request.getParameter("pass");
        String pass2 = request.getParameter("pass2");
        if(firstName == null || lastName == null || pass1 == null || pass2 == null) {
            failAdd(request, response);
            return;
        }
        
        if(!pass1.equals(pass2)) {
            failAdd(request, response);
            return;
        }
        
        validUserName valUN = new validUserName();
        validPW valPW = new validPW();
        
        if(!valUN.isValid(firstName+lastName)) {
            failAdd(request, response);
            return;
        }
        
        if(!valPW.isValid(pass2)) {
            failAdd(request, response);
            return;
        }
        
        UserWriter.addUser(firstName+lastName, pass2);
        response.sendRedirect("addStudent.jsp");
        return;
    }
                
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        process(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        process(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Handles login information";
    }
    
    private static String getElement(String query, String element) {
        QueryStringHandler QSH = new QueryStringHandler(query);
        return QSH.getElement(element);
    }
    
    private static String getUserName(HttpServletRequest request, HttpServletResponse response) {
        try {
            int i;
            Cookie[] cookies = request.getCookies();
            Cookie cookieUN = null;
            if(cookies.length == 0) {
                failAdd(request, response);
            }
            
            for(i=0; i<cookies.length; i++) {
                if(cookies[i].getName().equals("username"))
                    return cookies[i].getValue();
            }
            
            if(cookieUN == null) {
                failAdd(request, response);
            }
            return null;
        }
        catch(Exception ex) {
        }
        return null;
    }
}
